package hzn.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import hzn.util.MyRealm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    @Autowired
    MyRealm realm;

    SecurityManager securityManager(){
        DefaultWebSecurityManager m=new DefaultWebSecurityManager();
        m.setRealm(realm);
        return m;
    }

    @Bean
    ShiroFilterFactoryBean shiroFilterFactoryBean(){
        ShiroFilterFactoryBean b=new ShiroFilterFactoryBean();
        b.setSecurityManager(securityManager());
        //当用户访问必须认证才能访问的资源时，如果没认证就会跳转到这个请求中
        b.setLoginUrl("/showLogin");
        Map<String,String> m=new HashMap<>();
        m.put("/showRegist","anon");
        m.put("/user/regist","anon");
        m.put("/user/login","anon");
        m.put("/**","authc");
        b.setFilterChainDefinitionMap(m);
        return b;
    }

    //为在在前端使用shiro标签，必须向容器中添加下面对象
    @Bean
    public ShiroDialect s(){
        return new ShiroDialect();
    }
}
